package com.taobao.askwho.action;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.taobao.askwho.ao.UserAo;
import com.taobao.askwho.po.UserInfo;
import com.taobao.askwho.utils.arkclient.ArkDomain;
import com.taobao.askwho.utils.session.SessionUtil;
@Controller                 
@RequestMapping("/loginopt.do")
public class LoginAction extends BaseAction{
	Logger logger = Logger.getLogger(LoginAction.class);

	@RequestMapping(params = "method=login")
	public ModelAndView login(HttpServletRequest request){
		UserInfo user = SessionUtil.getUserInfo(request);
		System.out.println("user=" + user);
		if(user == null) {//未登陆
			String arkMail = ArkDomain.getArkUserEmail(request);
			user = UserAo.get().findUserByEmail(arkMail);
			if(user.getUuid() == null) {	//注册
				user = new UserInfo();
				user.setCreateTime(new Date());
				user.setLastLoginTime(new Date());
				user.setUserEmail(arkMail);
				user.setUuid(UUID.randomUUID().toString());
				user.setUserName(arkMail.substring(0,arkMail.lastIndexOf('@')));
				try {
					UserAo.get().addNewUser(user);
				} catch (Exception e) {
					logger.error("", e);
				}
			} else {
				//temp
			}
		} 
		SessionUtil.setUserInfo(request, user);
		ModelAndView view = new ModelAndView("/index");
		return view;	
	}	

	@RequestMapping(params = "method=update")
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String domainmail = ArkDomain.getArkUserEmail(request);
		String wangwang = request.getParameter("wangwang");
		String userName = request.getParameter("name");
		UserInfo user = UserAo.get().findUserByEmail(domainmail);
		String msg = "";
		if(user == null) {
			user = new UserInfo();
			user.setCreateTime(new Date());
			user.setLastLoginTime(new Date());
			user.setUserEmail(domainmail);
			user.setUserName(userName);
			user.setUserWangwang(wangwang);
			user.setVisitTimes(1);
			user.setUuid(UUID.randomUUID().toString());
			try {
				UserAo.get().addNewUser(user);
			} catch (Exception e) {
				logger.error("", e);
				msg = e.toString();
			}
		} else {
			user.setUserWangwang(wangwang);
			user.setUserName(userName);
			user.setLastLoginTime(new Date());
			user.setVisitTimes(user.getVisitTimes() + 1);
			try {
				UserAo.get().updateUser(user);
			} catch (Exception e) {
				logger.error("", e);
				msg = e.toString();
			}
		}

		ModelAndView view = new ModelAndView("/time/conf/user_info_update");
		view.addObject("msg", msg);
		view.addObject("userinfo", user);
		SessionUtil.setUserInfo(request, user);
		return view;
	}

	@RequestMapping(params = "method=display")
	public ModelAndView display(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView view = new ModelAndView("/time/conf/user_info_update");
		String mail = ArkDomain.getArkUserEmail(request);
		UserInfo user = UserAo.get().findUserByEmail(mail);
		if(user == null){//目前没有登录信息记录
			user = new UserInfo();
			user.setCreateTime(new Date());
			user.setLastLoginTime(new Date());
			user.setUserEmail(mail);
			user.setUserName("");
			user.setUserWangwang("");
			user.setVisitTimes(1);
			user.setUuid(UUID.randomUUID().toString());
		}
		view.addObject("userinfo", user);
		return view;
	}	

	//个人主页
	@RequestMapping(params = "method=gotoPersonPage")
	public String gotoPersonPage(HttpServletRequest request) {
		UserInfo user = SessionUtil.getUserInfo(request);
		if(user == null) {
			return "redirect:/urljump.do?method=gotoLoginPage";
		} else {
			return "redirect:/urljump.do?method=gotoPersonPage&userUuid=" + user.getUuid();
		}
	}	

	//收藏一个Tag
	@RequestMapping(params = "method=collectATag")
	public String collectATag(HttpServletRequest request, String tagUuid) {
		UserInfo user = SessionUtil.getUserInfo(request);
		if(user == null) {
			return "redirect:/urljump.do?method=gotoLoginPage";
		} else {
			return "redirect:/tagopt.do?method=collectATag&tagUuid=" + tagUuid;
		}
	}		

}
